﻿var Commerial = "";
$('#submit_button').click(function () {

    var f = document.getElementById('source_url3');
    var image_url = f.options[f.selectedIndex].text;
    lastSelectedImageUrl = image_url;
    test(f);


    $.post('/Handlers/MappingHandler.ashx', { "id": logoInfoId, "details": JSON.stringify(detailToDetail) }, function (d) {

        //console.log(d);

        if (d.result == "success") {
            alert('Your changes have been saved successfully.');

            window.location.href = "ToDayList.aspx";

        }
        else {
            alert('Some error occured while saving changes.');
            return false;

        }
    });
    //return false;
});

$(function () {
    var uploader = new qq.FileUploader({
        element: document.getElementById('file-uploader'),
        action: '/Handlers/UploadHandler.ashx',
        debug: true,
        async: false,
        allowedExtensions: ["png", "gif", "jpg"],
        listElement: document.getElementById('uploaded-files-response'),
        onComplete: function (id, fileName, responseJSON) {

            //console.log(responseJSON);

            var resultDiv = document.getElementById('file-upload-result');
            try {
                resultDiv.innerHTML = "File has been uploaded successfully, copy the url above and start using.";
            } catch (e) {

            }

        }
    });
});
//console.log(detailToDetail);
function getScriptFromJson(fileName) {
    for (var i = 0; i < detailToDetail.length; i++) {
        if (detailToDetail[i]["FileName"] == fileName) {
            //console.log(detailToDetail[i]);
            return detailToDetail[i];
        }
    }
}
function setScriptInJson(fileName, script) {
    for (var i = 0; i < detailToDetail.length; i++) {
        if (detailToDetail[i]["FileName"] == fileName) {
            //console.log(detailToDetail[i]);

            var map_id = myimgmap.getMapId();

            var imgtag = '<img usemap="#' + map_id + '"  src="' + detailToDetail[i]["FileName"] + '" alt="' + detailToDetail[i]["Description"] + '">';

            //script = script.replace(map_id, map_id + new Date().getMilliseconds());

            detailToDetail[i]["TempScript"] = script;
            detailToDetail[i]["Scripts"] = imgtag + script;
            //console.log(detailToDetail[i]);
            break;
        }
    }
}

function testingFunct() {
    var f = document.getElementById('source_url3');
    if (lastSelectedImageUrl) {
        setScriptInJson(lastSelectedImageUrl, document.getElementById('html_container').value);
    }
    var image_url = f.options[f.selectedIndex].text;
    // get object
    var detailObj = getScriptFromJson(image_url);
    //console.log(detailObj)
    lastSelectedImageUrl = image_url;

    // store temp script first
    // because it is updated by following method if not stored here
    var temp_script = detailObj["TempScript"] || (detailObj["Scripts"] && detailObj["Scripts"].substring(detailObj["Scripts"].indexOf('>') + 1)) || "";

    detailObj["MenuId"] = $("#ddlMenu").val();
}

var lastSelectedImageUrl = "";
function test(f) {
    if (lastSelectedImageUrl) {
        setScriptInJson(lastSelectedImageUrl, document.getElementById('html_container').value);
    }
    var image_url = f.options[f.selectedIndex].text;
    // get object
    var detailObj = getScriptFromJson(image_url);
    lastSelectedImageUrl = image_url;

    // store temp script first
    // because it is updated by following method if not stored here
    var temp_script = detailObj["TempScript"] || (detailObj["Scripts"] && detailObj["Scripts"].substring(detailObj["Scripts"].indexOf('>') + 1)) || "";

    var menu = detailObj["MenuId"];
    if (menu == null)
        menu = 0;
    $("#ddlMenu").val(menu);
    gui_loadImage(image_url);
    // use temp script here.
    document.getElementById('html_container').setAttribute('oldvalue', '');
    document.getElementById('html_container').value = temp_script;

    // update areas.
    gui_htmlBlur();
}

test(document.getElementById('source_url3'));

function onHtmlChanged_Custom(str) {

    //console.log('onHtmlChanged_Custom: ' + str);

    //            var f = document.getElementById('source_url3');
    //            var imageUrl = f.options[f.selectedIndex].text;
    //            setScriptInJson(imageUrl, str);


}
function CopyToClipboard(arg) {
    $(arg).zclip({
        path: "/Contents/ZeroClipboard.swf",
        copy: function () {
            return this.title;
        },
        afterCopy: function () {
            alert('The url has been copied to your clipboard.');
        }
    });

    return false;
}

var strFormate = '<li >' +
                '<span class="container1">' +
                '<span class="upload-file"></span>&nbsp;&nbsp;&nbsp;' +
                '<a class="previewClass" href="#" target="_blank">Preview</a>' +
                ' </span><span class="qq-upload-copy-container" >' +
                '<a class="copy" href="#" title="" target="_blank"> Copy</a> </span>' +
                 '<span class="upload-url"></span>' +
            '</li>'
$(document).ready(function () {
    $('#btnLoadImages').click(function () {
        CallFileInformationMenu();
    });
});

function SaveFileInformationInTable() {

}
$("#btnRefresh").click(function () {
    CallFileInformationMenu();
});

function CallFileInformationMenu() {
    jQuery("#OldFiles").html("");
    var objAjax = jQuery.ajax({
        type: "GET",
        url: '/Handlers/GetFileObjects.ashx',
        data: { date: $('#txtDate').val(), page: $("#PageNumberSelected option:selected").val() },
        dataType: "application/json",
        async: true,
        error: function (jqXHR, textStatus, errorThrown) {
            var parse1 = jQuery.parseJSON(jqXHR.responseText);
            for (var i = 0; i < parse1.FileInformations.length; i++) {
                var obj = $(strFormate);

                var preview = obj.find('.previewClass');
                var bnURL = "javascript:open_popup('/NewsDetail.aspx?newsid=" + parse1.FileInformations[i].Id + "')";
                var copy = obj.find('.copy');
                var uploadfile = obj.find('.upload-file');

                copy.attr('title', bnURL);
                var spanText = obj.find('.upload-url');
                CopyToClipboard('.copy');
                spanText.html(bnURL);
                uploadfile.html(parse1.FileInformations[i].ActualFileName);

                preview.attr("href", bnURL);
                preview.attr("onmousemove", "showTooltip(event, 'dvTooltip','" + parse1.FileInformations[i].CurrentFileName + "');");
                preview.attr("onmouseout", "hideTooltip(event, 'dvTooltip');");

                jQuery("#OldFiles").append(obj);
            }
            $('#PageNumberSelected').empty();
            for (var i = 0; i < parse1.PageNumbers.length; i++) {
                var newOption = $('<option value="' + parse1.PageNumbers[i] + '">' + parse1.PageNumbers[i] + '</option>');
                $('#PageNumberSelected').append(newOption);
            }
        },
        success: function (msg) {
            alert(msg.d[0].Name);
        }
    });
}

function GetElments(html) {
    var div = document.createElement('div');
    return function (html) {
        div.innerHTML = html;
        var element = div.firstChild;
        div.removeChild(element);
        return element;
    }
}
function showTooltip(e, tooltip, imagPath) {
    //    alert("");
    e = window.event ? window.event : e;
    tooltip = document.getElementById(tooltip);
    var toa = tooltip.getElementsByTagName("img");
    toa[0].setAttribute("src", imagPath);

    if (tooltip.style.display != "block")
        tooltip.style.display = "block";
    //    tooltip.style.left = e.clientX + "px";
    //    tooltip.style.top = e.clientY + "px";
}
function hideTooltip(e, tooltip) {
    //    alert("");
    e = window.event ? window.event : e;
    tooltip = document.getElementById(tooltip);
    if (e.toElement == tooltip) {
        showTooltip(e, tooltip);
        return;
    }
    tooltip.style.display = "none";
}

$(function () {

    $("#txtDate").datepicker();
});